package com.shitou.shitoumember.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoumember.entity.CaptchaEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shitou.shitoumember.service.CaptchaService;
import com.shitou.shitoucommons.utils.RespBean;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


/**
 *
 *
 * @author shitouren
 * @email 3076767823@qq.com
 * @date 2023-03-28 16:52:06
 */
@Api(value = "验证码控制")
@RestController
@RequestMapping("shitoumember/captcha")
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = captchaService.queryPage(params);

        return RespBean.success().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public RespBean info(@PathVariable("id") Long id){
        CaptchaEntity captcha = captchaService.getById(id);

        return RespBean.success().put("captcha", captcha);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody CaptchaEntity captcha){
        captchaService.save(captcha);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody CaptchaEntity captcha){
        captchaService.updateById(captcha);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] ids){
        captchaService.removeByIds(Arrays.asList(ids));

        return RespBean.success();
    }


    /**
     * 生成验证码
     * @param uuid uuid
     * @return void
     */
    @ApiOperation(value = "生成验证码")
    @GetMapping("/captcha.jpg")
    public void createCaptcha(String uuid, HttpServletResponse response) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = captchaService.createCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }


    /**
     * 发送邮箱验证码
     * @param uuid uuid
     * @param email 用户邮箱号s
     * @return RespBean
     */
    @ApiOperation(value = "发送邮箱验证码")
    @PostMapping("/sendMail")
    public RespBean sendMail(String uuid, String email){
        return captchaService.sendEmail(uuid, email);
    }


    /**
     * 检验邮箱是否注册，发送验证码
     * @param uuid uuid
     * @param email 用户邮箱号
     * @return RespBean
     */
    @ApiOperation("检验邮箱是否注册，发送验证码")
    @PostMapping("/sendCheckEmail")
    public RespBean sendCheckMail(String uuid, String email){
        return captchaService.sendCheckEmail(uuid, email);
    }


    /**
     * 邮箱验证码验证
     * @param uuid uuid
     * @param captcha 验证码
     * @param email 用户邮箱
     * @return RespBean
     */
    @ApiOperation(value = "邮箱验证码验证")
    @GetMapping("/validateEmailCatpcha")
    public RespBean validateEmailCaptcha(String uuid, String captcha, String email){
        return captchaService.validateEmail(uuid, captcha, email);
    }


    /**
     * 发送注册邮验证码
     * @param uuid
     * @param email
     * @return
     */
    @ApiOperation(value = "发送注册邮验证码")
    @PostMapping("/sendRegisterEmail")
    public RespBean sendRegisterEmail(@RequestParam String uuid, @RequestParam String email){
        return captchaService.sendRegisterEmail(uuid, email);
    }


}
